package main.java.org.mapper;

import main.java.Exam.pojo.Entity.ClassTeacher;
import main.java.Exam.pojo.Entity.exam;
import main.java.Exam.pojo.tableDTO.ExamDTO;
import main.java.org.result.TestQuestionListTypeHandler;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ExamMapper {
    //删除考试
    @Delete("DELETE FROM tb_exam WHERE testId = #{testId} AND userAccount=#{userAccount}")
    void delete(Long testId, String userAccount);
    //添加考试
    @Insert("INSERT INTO tb_exam (userAccount,testTitle,testDescription,totalScore,startTime, endTime, testQuestions, createrName,testStatus,testTime ) " +
            "VALUES ( #{userAccount},#{testTitle},#{testDescription},#{totalScore},#{startTime},#{endTime},#{testQuestions,typeHandler=main.java.org.result.TestQuestionListTypeHandler},#{createrName},#{testStatus},#{testTime})")
    void add(ExamDTO examDTO);
    //编辑考试
    @Update("UPDATE tb_exam SET testTitle = #{testTitle},testDescription = #{testDescription},totalScore = #{totalScore},startTime = #{startTime}, " +
            "endTime = #{endTime},testQuestions = #{testQuestions,typeHandler=main.java.org.result.TestQuestionListTypeHandler},testStatus = #{testStatus},testTime = #{testTime} " +
            "WHERE testId = #{testId}")
    void update(ExamDTO examDTO);
    //获取老师自己的所有考试信息
    @Select("select testId, testTitle, totalScore, testTime, startTime, endTime, testStatus,testQuestions FROM tb_exam WHERE userAccount = #{userAccount}")
    @Results({
            @Result(column = "testQuestions", property = "testQuestions",
                    typeHandler = TestQuestionListTypeHandler.class)
    })
    List<ExamDTO> getexam(String userAccount);
    //学生获取所有考试
    @Select("select testId, testTitle,testDescription, totalScore, testTime, startTime, endTime, testStatus,createrName ,testQuestions FROM tb_exam ")
                  @Results({
            @Result(column = "testQuestions", property = "testQuestions",
                    typeHandler = TestQuestionListTypeHandler.class)
    })
    List<ExamDTO> getuserexam();
    //获取单个考试
    @Select("select testId, testTitle, totalScore, testTime, startTime, endTime, testStatus,testQuestions from tb_exam where testId=#{testId}")
    @Results({
            @Result(column = "testQuestions", property = "testQuestions",
                    typeHandler = TestQuestionListTypeHandler.class)
    })
    exam getIdTest(Integer testId);

    @Select("SELECT ct.username,ct.class_id,ct.teacher_id,ct.student_id,ct.test_id, " +
        "ct.test_title,ct.score,ct.scorerank,ct.useraccount,et.past_time AS pastTime,et.submit_time AS submitTime  " +
        "FROM tb_class_teacher ct LEFT JOIN tb_exam_time_record et " +
        "ON ct.test_id = et.test_id AND ct.student_id = et.user_id  " +
        "WHERE ct.test_id = #{testId}")
    List<ClassTeacher> getstudentexam(Integer teseId);
//最高分
    @Select("select max(score) from tb_class_teacher where test_id=#{testId}")
    int geimax(Integer testId);
    //最低分
    @Select("select min(score) from tb_class_teacher where test_id=#{testId}")
    int getmin(Integer testId);
    //平均分
    @Select("select avg(score) from tb_class_teacher where test_id=#{testId}")
    int average(Integer testId);
    //及格人数
    @Select("select count(score) from tb_class_teacher where score>60 and test_id=#{testId}")
    int pass(Integer testId);


@Update("UPDATE tb_class_teacher SET score = score + #{stuScore} WHERE test_id=#{testId} and userAccount=#{userAccount} ;")
    void getscore(Integer stuScore, Integer testId, String userAccount);
@Select("select score from tb_class_teacher where userAccount=#{userAccount} and test_id=#{testId}")
    Integer getxx(String userAccount, Integer testId);
    @Select("select userName from tb_class_teacher where userAccount=#{userAccount} and test_id=#{testId}")
    String getaaa(Integer stuScore, Integer testId, String userAccount);
}
